/*
Written by Best Flash Solutions, Inc  
See:
	http://www.bestflashsolutions.com
Contact me: contact@bestflashsolutions.com
*/

/**
 * Create Panel (Window) with Minimize, Maximize and Resiziable 
 */
package com.bestflashsolutions.components.MyPanel
{
	import com.bestflashsolutions.components.ChatWindowLayoutCanvas;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.containers.Panel;
	import mx.core.Application;
	import mx.core.Container;
	import mx.core.SpriteAsset;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	
	public class DragPanel extends Panel
	{
		// Add the creationCOmplete event handler.
		
		public function DragPanel()
		{
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}
		
		// Expose the title bar property for draggin and dropping.
		[Bindable]
		public var myTitleBar:UIComponent;
		public var rubberBand: RubberBandComp;
		public var objParent: Container;
		public var moveable:   Boolean = false ;
		public var resizeable: Boolean = false ;
		private var controlDisalbed=true;
		
		public const minHorSpace:Number = 100;
		public const minVerSpace:Number = 75;
		
		
		private function creationCompleteHandler(event:Event):void
		{
			myTitleBar = titleBar;			
			// Add the resizing event handler.	
			
			if(!controlDisalbed){
				addEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
			}
			this.layout = "absolute";
			myTitleBar.addEventListener(MouseEvent.MOUSE_DOWN, movePanelHandler);
			addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
			addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
			
			setStyle("cornerRadius",0);
			setStyle("roundedBottomCorners",false);
		}
		
		public function enableControl(val:Boolean):void{
			controlDisalbed = !val;
			if(controlDisalbed){
				removeEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
				moveable = false;
				resizeable = false;
			}else{
				addEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
				moveable = true;
				resizeable = true;
			}
			updateDisplayList(width,height);
		}
		
		
		var oldColor: Number;
		//Show focus
		public function focus(v: Boolean) {
			if (v) {
				oldColor = myTitleBar.getStyle("backgroundColor");
				myTitleBar.setStyle("backgroundColor", 0xFF00FF);
			}
			else
				myTitleBar.setStyle("backgroundColor", oldColor);
		}
		
		private function onFocusIn(e: Event) {
			focus(true);
		}

		private function onFocusOut(e: Event) {
			focus(false);			
		}
		
		protected var minShape:SpriteAsset;
		protected var restoreShape:SpriteAsset;
		protected var resizeShape:Canvas;
		
		override protected function createChildren():void
		{
				super.createChildren();
			
			// Create the SpriteAsset's for the min/restore icons and 
			// add the event handlers for them.
			minShape = new SpriteAsset();
			//Add Minizie shape
			minShape.addEventListener(MouseEvent.MOUSE_DOWN, minPanelSizeHandler);
			titleBar.addChild(minShape);

			restoreShape = new SpriteAsset();
			//Add Maximize shape
			restoreShape.addEventListener(MouseEvent.MOUSE_DOWN, restorePanelSizeHandler);
			titleBar.addChild(restoreShape);
			
			resizeShape =  new Canvas();
			resizeShape.styleName = "cnvResizeCorner"
			resizeShape.width = resizeShape.height = 25;
			resizeShape.mouseEnabled=false;
			resizeShape.mouseChildren=false;
			rawChildren.addChild(resizeShape)
		}
			
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{	
			/*if (unscaledHeight <= minVerSpace)
				unscaledHeight = minVerSpace;  
			
			if (unscaledWidth <= minHorSpace)
				unscaledWidth = minHorSpace;*/			
			
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			// Create invisible rectangle to increase the hit area of the min icon.
			minShape.graphics.clear();
			restoreShape.graphics.clear();
			if(!controlDisalbed){
				
				if(!isMinimized){
					minShape.graphics.lineStyle(0, 0x111111, 0);
					minShape.graphics.beginFill(0xFFFFFF, 0.0);
					minShape.graphics.drawRect(unscaledWidth - 19, 7, 8, 8);
		
					// Draw min icon.
					minShape.graphics.lineStyle(2,0x111111);
					minShape.graphics.beginFill(0xFFFFFF, 0.0);
					minShape.graphics.drawRect(unscaledWidth - 19, 13, 8, 2);
				}
				// Draw restore icon.
				if(isMinimized){
					restoreShape.graphics.lineStyle(2,0x111111);
					restoreShape.graphics.beginFill(0xFFFFFF, 0.0);
					restoreShape.graphics.drawRect(unscaledWidth - 20, 8, 7, 7);
					restoreShape.graphics.endFill();
					restoreShape.graphics.beginFill(0xFFFFFF, 0.0);
					restoreShape.graphics.drawRect(unscaledWidth - 17, 5, 7, 7);
					restoreShape.graphics.endFill();
				}
				
			}
			// Draw resize graphics if not minimzed.				
			//graphics.clear()
			if (isMinimized == false && !controlDisalbed){	resizeShape.visible = true
				resizeShape.x = unscaledWidth-resizeShape.width;
				resizeShape.y = unscaledHeight-resizeShape.height;
			}else{
				resizeShape.visible=false
			}
			
				
			
		}
					
		private var myRestoreHeight:int;
		private var isMinimized:Boolean = false; 
		
		// Minimize panel event handler.
		private function minPanelSizeHandler(event:Event):void
		{
			if (isMinimized != true)
			{
				myRestoreHeight = height;	
				height = titleBar.height;
				isMinimized = true;	
				// Don't allow resizing when in the minimized state.
				removeEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
				
				minShape.graphics.clear();
				restoreShape.graphics.clear();
				restoreShape.graphics.lineStyle(2,0x111111);
				restoreShape.graphics.beginFill(0xFFFFFF, 0.0);
				restoreShape.graphics.drawRect(unscaledWidth - 20, 8, 7, 7);
				restoreShape.graphics.endFill();
				restoreShape.graphics.beginFill(0xFFFFFF, 0.0);
				restoreShape.graphics.drawRect(unscaledWidth - 17, 5, 7, 7);
				restoreShape.graphics.endFill();
				
			}				
		}
		
		// Restore panel event handler.
		private function restorePanelSizeHandler(event:Event):void
		{
			if (isMinimized == true)
			{
				height = myRestoreHeight;
				isMinimized = false;	
				// Allow resizing in restored state.				
				addEventListener(MouseEvent.MOUSE_DOWN, resizeHandler);
				
				minShape.graphics.clear();
				restoreShape.graphics.clear();
				minShape.graphics.lineStyle(0, 0x111111, 0);
				minShape.graphics.beginFill(0xFFFFFF, 0.0);
				minShape.graphics.drawRect(unscaledWidth - 19, 7, 8, 8);
				minShape.graphics.lineStyle(2,0x111111);
				minShape.graphics.beginFill(0xFFFFFF, 0.0);
				minShape.graphics.drawRect(unscaledWidth - 19, 13, 8, 2);
					
			}
		}

		public function forceMinimize() {
			minPanelSizeHandler( null);
		}
		
		public function forceMaximize() {
			restorePanelSizeHandler(null);
		}

		//Width of container area
		public function get renderWidth(): Number {
			
			return width//-10//width - getStyle("borderThicknessLeft")-getStyle("borderThicknessRight")-4;	
		}
		
		//Height of container area
		public function get renderHeight(): Number {
			return height - titleBar.height - 8;	
		}
				
		// Define static constant for event type.
		public static const RESIZE_CLICK:String = "resizeClick";
		protected var initX:Number;
		protected var initY:Number;

		// Resize panel event handler.
		public  function resizeHandler(event:MouseEvent):void
		{
			if (resizeable==false) return;
			
			// Determine if the mouse pointer is in the lower right 7x7 pixel
			// area of the panel. Initiate the resize if so.
			
			// Lower left corner of panel
			var lowerLeftX:Number = x + width; 
			var lowerLeftY:Number = y + height;
				
			// Upper left corner of 7x7 hit area
			var upperLeftX:Number = lowerLeftX-25;
			var upperLeftY:Number = lowerLeftY-25;
				
			// Mouse positionin Canvas
			var panelRelX:Number = event.localX + x;
			var panelRelY:Number = event.localY + y;

			// See if the mousedown is in the lower right 7x7 pixel area
			// of the panel.
			if (upperLeftX <= panelRelX && panelRelX <= lowerLeftX)
			{
				if (upperLeftY <= panelRelY && panelRelY <= lowerLeftY)
				{		
					event.stopPropagation();
					
					initX = event.stageX;
					initY = event.stageY;

					// Place the rubber band over the panel. 
					rubberBand.x = x;
					rubberBand.y = y;
					rubberBand.height = height;
					rubberBand.width = width;
					
					// Make sure rubber band is on top of all other components.
					
					rubberBand.visible=true;

					systemManager.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler, true);
					systemManager.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, true);

				}
			}	
			
			if(Application.application.cvsRoom.layout == ChatWindowLayoutCanvas.LAYOUT_FLOAT)
				parent.setChildIndex(this, parent.numChildren-1);
			parent.setChildIndex(rubberBand, parent.numChildren-1);
		}
		
		// Resizes the rubber band as the user moves the cursor 
		// with the mouse key down.
		protected function mouseMoveHandler(event:MouseEvent):void
		{
				event.stopImmediatePropagation();	
				
				rubberBand.width = rubberBand.width + event.stageX - initX;
				rubberBand.height = (rubberBand.width/width) * height;            //rubberBand.height + event.stageY - initY;  
				
				
				initX = event.stageX;
				initY = event.stageY;						
		}
		
		// Sizes the panel to the size of the rubber band when the 
		// user releases the mouse key. 
		// Also removes the event handlers from the SystemManager.
		protected function mouseUpHandler(event:MouseEvent):void
		{
					
            
			rubberBand.width = Math.round(rubberBand.width + event.stageX - initX);
			rubberBand.height = Math.round((rubberBand.width/width) * height);
			
			// Use a minimum panel size of 150 x 50.
			if (rubberBand.height <= minVerSpace)
				height = minVerSpace;  
			else
				height = rubberBand.height;  				
			
			if (rubberBand.width <= minHorSpace)
				width = minHorSpace;				
			else
				width = rubberBand.width;		
			
			event.stopImmediatePropagation();
			
			// Put the resized panel on top of all other components.
			if(Application.application.cvsRoom.layout == ChatWindowLayoutCanvas.LAYOUT_FLOAT)
				parent.setChildIndex(this, parent.numChildren-1);
			parent.setChildIndex(rubberBand, parent.numChildren-1); 
			
			// Hide the rubber band until next time.
			rubberBand.x = 0;
			rubberBand.y = 0;
			rubberBand.height = 0;
			rubberBand.width = 0;
			rubberBand.visible = false;
			
			systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler, true);
			systemManager.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, true	);	
		}
		
		//Process when start Movepanel
		private function movePanelHandler(event: MouseEvent) { trace("movePanelHandler"+parent)
			if (moveable==false) return;
			event.stopPropagation();
			
			initX = event.stageX;
			initY = event.stageY;
            
			if(Application.application.cvsRoom.layout == ChatWindowLayoutCanvas.LAYOUT_FLOAT)
				parent.setChildIndex(this, parent.numChildren-1);
			parent.setChildIndex(rubberBand, parent.numChildren-1);
			
			systemManager.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveMovePanelHandler, true);
			systemManager.addEventListener(MouseEvent.MOUSE_UP, mouseUpMovePanelHandler, true);			
		}
		
		
		// Resizes the rubber band as the user moves the cursor 
		// with the mouse key down.
		protected function mouseMoveMovePanelHandler(event:MouseEvent):void
		{
			event.stopImmediatePropagation();		
			var newPosX: Number = event.stageX;
			var newPosY: Number = event.stageY;

			x += Math.round(newPosX - initX);
			y += Math.round(newPosY - initY);	

			initX = newPosX;
			initY = newPosY;
		}
		
		// Sizes the panel to the size of the rubber band when the 
		// user releases the mouse key. 
		// Also removes the event handlers from the SystemManager.
		protected function mouseUpMovePanelHandler(event:MouseEvent):void
		{
			event.stopImmediatePropagation();		

			// Put the resized panel on top of all other components.
			

			systemManager.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveMovePanelHandler, true);
			systemManager.removeEventListener(MouseEvent.MOUSE_UP, mouseUpMovePanelHandler, true	);	
		}
		
		override public function set width(value:Number):void{
			super.width = value;
		} 

	}
}